﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Threading;
using System.Windows.Threading;

using System.Diagnostics;
using Bohdan.Arch.Messaging;

namespace WPF_Testing_Application
{
    public class UIMessage : GeneralMessage
    {
        public override Guid TargetSubscriberId
        {
            get
            {
                return UIWorker._Id;
            }
        }

        public UIMessage()
        {
        }

        public UIMessage(string logmessage)
        {
            this.LogMessage = logmessage;
        }

        public string LogMessage { get; set; }
    }

    public class UICustomDeletage : GeneralMessage
    {
        public override Guid TargetSubscriberId
        {
            get { return UIWorker._Id; }
        }

        public UICustomDeletage(Action action)
        {
            this.Action = action;
        }

        public Action Action { get; set; }
    }

    public class UIShowLongMessage : GeneralMessage
    {
        public override Guid TargetSubscriberId
        {
            get { return UIWorker._Id; }
        }
    }

    public class UIHideLongMessage : GeneralMessage
    {
        public override Guid TargetSubscriberId
        {
            get { return UIWorker._Id; }
        }
    }

    public class UIExitMessage : GeneralExitMessage
    {
        public override Guid TargetSubscriberId
        {
            get
            {
                return UIWorker._Id;
            }
        }
    }

    public class UIWorker : GeneralDispatcherSubscriber
    {
        public readonly static Guid _Id = new Guid("{6033BB8A-37E8-4101-9C02-2A60EF63D182}");
        public override Guid SubscriberId
        {
            get
            {
                return _Id;
            }
        }

        public wndMain TargetWindow { get; set; }

        public override System.Threading.ThreadPriority WorkerThreadPriority
        {
            get
            {
                return System.Threading.ThreadPriority.AboveNormal;
            }
        }

        public UIWorker(wndMain window)
            : base(window.Dispatcher)
        {
            this.TargetWindow = window;
        }

        protected override void ProcessMessage(GeneralMessage msg)
        {
            UIMessage m;
            UIExitMessage exM;
            if (msg is UIShowLongMessage)
                TargetWindow.BeginReportLong();
            else if (msg is UIHideLongMessage)
                TargetWindow.EndReportLong();
            else if ((m = msg as UIMessage) != null)
                TargetWindow.Log(m.LogMessage);
            else if ((exM = msg as UIExitMessage) != null)
                TargetWindow.IntentionalClose();
            else if (msg is UICustomDeletage)
                ((UICustomDeletage)msg).Action();
        }
    }
}
